//
//  ContactViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/22.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SVProgressHUD
import SwiftyJSON

class ContactViewController: UIViewController,UITextViewDelegate {

    let ideaText = UITextView()
    
    let nameTextField = UITextField()
    let emailTextField = UITextField()
    
    let placeLabel = UILabel()
    
    let rightBtn = UIButton(type: .Custom)
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.creatNavVIew()
        self.creatView()
        
    }
    
    
}


extension ContactViewController{
    
    private func creatNavVIew(){
        self.automaticallyAdjustsScrollViewInsets = false
        self.view.backgroundColor = UIColor.whiteColor()
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 94/255.0, green: 200/255.0, blue: 252/255.0, alpha: 1)
        self.title = "CONTACT US"
        
        let leftBtn = UIButton(type: .Custom)
        leftBtn.setImage(UIImage(named: "nav_back"), forState: .Normal)
        leftBtn.frame = CGRectMake(0, 0, 30, 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), forControlEvents: .TouchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
        
        
        
        rightBtn.setTitle("Send", forState: .Normal)
        rightBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        rightBtn.frame = CGRectMake(0, 0, 50, 30)
        rightBtn.addTarget(self, action: #selector(rightBtnClick(_:)), forControlEvents: .TouchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
        
    }
    
    func leftBtnClick(){
        self.dismissViewControllerAnimated(true) {
            
        }
    }
    
    func rightBtnClick(button:UIButton){
        
        if button.selected == true {
            SVProgressHUD.showInfoWithStatus("Please send after 5 seconds!")
            return
        }else{
            button.selected = true
            
            self.performSelector(#selector(buttonSelected(_:)), withObject: nil, afterDelay: 5.0)
            
            if self.judge() == "yes" {
                
                let urlStr = APIBASE + "main_page=contact_us"
                let parma = ["contactname":nameTextField.text!,"email":emailTextField.text!,"enquiry":ideaText.text!]
                NetworkTool.shareInstance.request(.POST, urlString: urlStr, parmas: parma, finishedBlock: { (result, error) in
                    if result != nil{
                        print(result)
                        let number = String((result! as? NSDictionary)!["status"]!)
                        if number == "1"{
                            SVProgressHUD.showSuccessWithStatus("Send successfully!")
                        }else{
                            SVProgressHUD.showInfoWithStatus((result as? NSDictionary)!["status_message"]as?String)
                        }
                        
                    }else{
                        SVProgressHUD.showErrorWithStatus("get currency data error:" + String(error))
                        print("get currency data error:" + String(error))
                    }
                })
                
                
            }else{
                SVProgressHUD.showInfoWithStatus(self.judge())
            }
            
        }
        
    }
    func judge() -> String{
        var str:String?
        
        if ideaText.text == nil || ideaText.text == "" {
            str = "Please fill in your comments or Suggestions!"
        }else if nameTextField.text == nil || nameTextField.text == ""{
            str = "Please fill in your name!"
        }else if emailTextField.text == nil || emailTextField.text == ""{
            str = "Please fill in your email!"
        }else{
            str = "yes"
        }
        return str!
    }
    
    func buttonSelected(button:UIButton){
        rightBtn.selected = false
    }
}
extension ContactViewController{
    
    private func creatView(){
        
        ideaText.frame = CGRectMake(20, 74, SCREEN_WIDTH-40, SCREEN_HEIGHT*0.4)
        ideaText.font = UIFont.systemFontOfSize(17)
        ideaText.delegate = self
        self.view.addSubview(ideaText)
        
        placeLabel.frame = CGRectMake(5, 0, SCREEN_WIDTH-40, 40)
        placeLabel.text = "What can we do to help you?"
        placeLabel.textColor = UIColor.grayColor()
        ideaText.addSubview(placeLabel)
        
        
        nameTextField.frame = CGRectMake(20, 74+SCREEN_HEIGHT*0.4, SCREEN_WIDTH/2-20, SCREEN_HEIGHT*0.06)
        nameTextField.placeholder = " Name"
        nameTextField.layer.borderWidth = 1
        nameTextField.layer.borderColor = UIColor.grayColor().CGColor
        self.view.addSubview(nameTextField)
        
        emailTextField.frame = CGRectMake(SCREEN_WIDTH/2, 74+SCREEN_HEIGHT*0.4, SCREEN_WIDTH/2-20, SCREEN_HEIGHT*0.06)
        emailTextField.placeholder = " Email"
        emailTextField.layer.borderColor = UIColor.grayColor().CGColor
        emailTextField.layer.borderWidth = 1
        self.view.addSubview(emailTextField)
        
        
    }
    
    
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        if text.characters.count > 0 {
            placeLabel.hidden = true
        }else{
            placeLabel.hidden = false
        }
        return true
    }
}



























